﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using SmoothDraw;

namespace AlphaPaint
{
    public partial class ApController : Form
    {
        internal SmoothDrawModel model;

        public ApController ()
        {
            ApView view;

            InitializeComponent();
            model = new SmoothDrawModel();
            view = new ApView (this, model);
        }

        private void picPanel_Paint (object sender, PaintEventArgs pArgs)
        {
            var ts = 16;
            using (var darkBrush = new SolidBrush (Color.LightGray))
            using (var lightBrush = new SolidBrush (Color.Gainsboro))
            {
                for (var x = 0; x < picPanel.Size.Width/ts; ++x)
                    for (var y = 0; y < picPanel.Size.Height/ts; ++y)
                        pArgs.Graphics.FillRectangle ((x+y)%2==0? lightBrush:darkBrush, x*ts, y*ts, ts, ts);

                if (model != null && model.Pic != null)
                {
                    if (picPanel.Size.Width != model.PicSize.Width || picPanel.Size.Height != model.PicSize.Height)
                        picPanel.Size = new Size (model.PicSize.Width, model.PicSize.Height);

                    pArgs.Graphics.DrawImage (model.Pic, Point.Empty);
                }
            }
        }

        private void picPanel_MouseClick (object sender, MouseEventArgs mArgs)
        {
            if (mArgs.Button == MouseButtons.Left)
            {
                model.MgColor = string.IsNullOrWhiteSpace (mgTextBox.Text)? Color.Transparent : Color.FromName (mgTextBox.Text);
                model.FloodTransparency (mArgs.X, mArgs.Y);
            }
            else if (mArgs.Button == MouseButtons.Right)
            {
                model.MgColor = string.IsNullOrWhiteSpace (mgTextBox.Text)? Color.Transparent : Color.FromName (mgTextBox.Text);
                model.Flood (mArgs.X, mArgs.Y);
            }
        }

        private void picPanel_MouseLeave (object sender, EventArgs e)
        {
            status1.Text = "";
            status2.Text = "";
        }

        private void picPanel_MouseMove (object sender, MouseEventArgs mArgs)
        {
            string result = "(" + mArgs.X + "," + mArgs.Y + ")";

            if (model.Pic != null && mArgs.X < model.Pic.Size.Width && mArgs.Y < model.Pic.Size.Height)
            {
                var dot = model.Pic.GetPixel (mArgs.X, mArgs.Y);
                status2.Text = string.Format ("{0,2:x2}{1,2:x2}{2,2:x2}{3,2:x2}", dot.A, dot.R, dot.G, dot.B);
            }

            status1.Text = result;
        }

        //// //// //// //// 

        private void copyButton_Click (object sender, EventArgs e)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                model.Pic.Save (ms, ImageFormat.Png);
                Clipboard.Clear();
                DataObject cbData = new DataObject();
                cbData.SetData ("PNG", true, ms);
                Clipboard.SetDataObject (cbData, true);
            }
        }

        private void drawButton_Click (object sender, EventArgs e)
        {
            float factorOut;
            model.BgColor = string.IsNullOrWhiteSpace (bgTextBox.Text)? Color.Transparent : Color.FromName (bgTextBox.Text);
            model.FgColor = Color.FromName (fgTextBox.Text);
            model.MgColor = string.IsNullOrWhiteSpace (mgTextBox.Text)? Color.Transparent : Color.FromName (mgTextBox.Text);
            model.Text = textTextBox.Text;
            model.PicSize = new Size ((int) sizeControl.Value, (int) sizeControl.Value);
            if (float.TryParse (factorTextBox.Text, out factorOut))
                model.FontFactor = factorOut;
            model.FontName = fontTextBox.Text;
            model.DrawPicture();
        }

        private void fillButton_Click (object sender, EventArgs e)
        {
            model.MgColor = string.IsNullOrWhiteSpace (mgTextBox.Text)? Color.Transparent : Color.FromName (mgTextBox.Text);
            model.FillMiddleground();
        }

        private void saveButton_Click (object sender, EventArgs e)
        {
            using (var dialog = new SaveFileDialog())
            {
                dialog.Filter = @"Ping files (*.png)|*.png";
                dialog.RestoreDirectory = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = new FileStream (dialog.FileName, FileMode.OpenOrCreate);
                    model.Pic.Save (fs, ImageFormat.Png);
                    fs.Close();
                }
            }
        }
    }
}
